package com.bkitmobile.kesu.android.GUI;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import com.bkitmobile.kesu.android.ClassInfo.UserConfig;
import com.bkitmobile.kesu.android.connectServer.staticClass;
import com.bkitmobile.kesu.android.service.UpdateLocationService;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class SettingGeneral extends Activity {
	private Spinner spinerChat, spinerPosition;
	private Button btnSet;
	private ChoiceDialog dlgChoice = null;
	private ChoiceInfo []ci;
	private UserConfig uConfig;
	private HashMap<String, ChoiceInfo> mapType;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_tab_general);
		uConfig = (UserConfig) getIntent().getExtras().getSerializable("config");
		mapType = new HashMap<String, ChoiceInfo>();
		init();
		
	}
	
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		int chat = 0,place = 0;
		if (spinerChat.getSelectedItemPosition()==0)
		{
			chat = UserConfig.RING_AND_VIBRATE;
		}
		else if(spinerChat.getSelectedItemPosition()==1)
		{
			chat = UserConfig.RING;
		} else if(spinerChat.getSelectedItemPosition()==2)
		{
			chat = UserConfig.VIBRATE;
		} else if(spinerChat.getSelectedItemPosition()==3)
		{
			chat = UserConfig.SILENT;
		} 
		
		if (spinerPosition.getSelectedItemPosition()==0)
		{
			place = UserConfig.RING_AND_VIBRATE;
		}
		else if(spinerPosition.getSelectedItemPosition()==1)
		{
			place = UserConfig.RING;
		} else if(spinerPosition.getSelectedItemPosition()==2)
		{
			place = UserConfig.VIBRATE;
		} else if(spinerPosition.getSelectedItemPosition()==3)
		{
			place = UserConfig.SILENT;
		} 
		JSONObject jObject = new JSONObject();
		try {
			jObject.put("id", "SettingGeneral");
			jObject.put("alertChat", chat);
			jObject.put("alertPlace", place);
			JSONStringer js = new JSONStringer();
			js.array();
			ArrayList<String> lsInfo = new ArrayList<String>();
			if (dlgChoice!=null) 
			{
				ci = ((ChoiceAdapter)dlgChoice.getGrid().getAdapter()).getListInfo();
				
				for (int i = 0 ; i < ci.length ; i++)
				{
					if (ci[i].isSelecting())
					{
						js.object().key("type").value(ci[i].getType()).endObject();
						lsInfo.add(ci[i].getType());
					}
				}
			} else {
				for (int i = 0 ; i < uConfig.getListType().size() ; i++)
				{
						js.object().key("type").value(uConfig.getListType().get(i)).endObject();
				}
			}
			
			js.endArray();
			jObject.put("listType", js.toString());
			Bundle data = new Bundle();
			data.putInt("alertChat", chat);
			data.putInt("alertPlace", place);
			if (lsInfo.size() >0 )data.putSerializable("listtype", lsInfo);
			else data.putSerializable("listtype", (ArrayList<String>) uConfig.getListType());
			Message msg = new Message();
			msg.setData(data);
			msg.what = UpdateLocationService.GENERAL;
			Log.d("send", jObject.toString());
			staticClass.socket.println(jObject.toString());
			staticClass.sendToService.sendMessage(msg);
		} catch (JSONException e)
		{
			
		} finally {
			
		}
	}


	private void init(){
		
		
		
		spinerChat = (Spinner) findViewById(R.id.setting_tabGeneral_chatSpiner);
		spinerPosition = (Spinner) findViewById(R.id.setting_tabGeneral_positionSpiner);

		btnSet = (Button) findViewById(R.id.setting_tab_general_setType);
		ci = buildChoiceInfo();
		String []arrayForSpinner = {getString(R.string.setting_spinner_ringAndVibrate),
									getString(R.string.setting_spinner_ringOnly), 
									getString(R.string.setting_spinner_vibrateOnly), 
									getString(R.string.setting_spinner_silent)};
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this, 
				android.R.layout.simple_dropdown_item_1line, arrayForSpinner);
		spinerChat.setAdapter(aa);
		spinerPosition.setAdapter(aa);
		if (uConfig.getAlertChat() == UserConfig.RING)
		{
			spinerChat.setSelection(1);
		} else if (uConfig.getAlertChat() == UserConfig.VIBRATE)
		{
			spinerChat.setSelection(2);
		} else if (uConfig.getAlertChat() == UserConfig.SILENT)
		{
			spinerChat.setSelection(3);
		} else if (uConfig.getAlertChat() == UserConfig.RING_AND_VIBRATE)
		{
			spinerChat.setSelection(0);
		}
		
		if (uConfig.getAlertPlace() == UserConfig.RING)
		{
			spinerPosition.setSelection(1);
		} else if (uConfig.getAlertPlace() == UserConfig.VIBRATE)
		{
			spinerPosition.setSelection(2);
		} else if (uConfig.getAlertPlace() == UserConfig.SILENT)
		{
			spinerPosition.setSelection(3);
		} else if (uConfig.getAlertPlace() == UserConfig.RING_AND_VIBRATE)
		{
			spinerPosition.setSelection(0);
		}
		btnSet.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(dlgChoice == null){
					dlgChoice = new ChoiceDialog(SettingGeneral.this);
					dlgChoice.show(ci);
					Log.d("Click", "" + ci);
				}
				else{
					Log.d("Click", "not null");
					dlgChoice.show(null);
				}
				
			}
		});
	}
	
	//get from database saved instance here
	//anh lam cai get tu database o day nha.
	private ChoiceInfo []buildChoiceInfo(){
		String []str = getResources().getStringArray(R.array.map_items);
		ChoiceInfo []ci = new ChoiceInfo[str.length];
		for(int i = 0; i < ci.length; i++){
			ci[i] = new ChoiceInfo(str[i]);
			mapType.put(ci[i].getType(), ci[i]);
		}
		for (int j = 0 ; j < uConfig.getListType().size() ; j++)
		{
			Log.d("type"+j, uConfig.getListType().get(j));
			ChoiceInfo cInfo = mapType.get(uConfig.getListType().get(j));
			if (cInfo != null) cInfo.setSelecting(true);
			else Log.d("type", uConfig.getListType().get(j));
		}
		return ci;
	}
	
}
